package yf.mall.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;

import java.util.Map;

import com.yf.consts.DaoConst;
import yf.mall.entity.LevelEntity;
import yf.mall.entity.MallActEntity;
import yf.mall.entity.MallGoodsEntity;
import yf.mall.service.LevelService;
import yf.mall.service.MallActService;
import com.yf.response.R;
import yf.mall.service.MallGoodsService;
import yf.mall.service.SumService;


/**
 * 活动Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Mon Nov 25 13:53:21 CST 2019
 */
@RestController
@RequestMapping("/mall/act")
public class MallActController {
    @Autowired
    private MallActService mallActService;

    @Autowired
    private MallGoodsService goodsService;

    @Autowired
    private LevelService levelService;

    /**
     * 列表活动
     */
    @RequestMapping("/list")
    @RequiresPermissions("mall:act:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageHelper.startPage(DaoConst.getPage(params), DaoConst.getLIMIT(params));
        List<MallActEntity> mallActList = mallActService.queryList(params);
        List<MallGoodsEntity> goodsEntities = goodsService.queryList(null);
        List<LevelEntity> levelEntities = levelService.queryList(null);
        return R.ok(new PageInfo(mallActList)).putExtra("goodsList", goodsEntities).putExtra("levelList", levelEntities);
    }

    /**
     * 查看活动
     */
    @SysLog("查看活动")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:act:info")
    public R info(@PathVariable("id") Long id) {
        MallActEntity mallAct = mallActService.get(id);
        MallGoodsEntity goodsEntity = goodsService.get(mallAct.getGoodsId());
        mallAct.setGoodsName(goodsEntity.getName());

        return R.ok(mallAct);
    }

    /**
     * 保存活动
     */
    @SysLog("保存活动")
    @RequestMapping("/save")
    @RequiresPermissions("mall:act:save")
    public R save(MallActEntity mallAct) {
        mallActService.save(mallAct);
        return R.ok(mallAct);
    }

    /**
     * 修改活动
     */
    @SysLog("修改活动")
    @RequestMapping("/update")
    @RequiresPermissions("mall:act:update")
    public R update(MallActEntity mallAct) {
        mallActService.update(mallAct);

        return R.ok(mallAct);
    }

    /**
     * 删除活动
     */
    @SysLog("删除活动")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:act:delete")
    public R delete(Long[] ids) {
        mallActService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有活动
     */
    @SysLog("所有活动")
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<MallActEntity> mallActList = mallActService.queryList(params);

        return R.ok(mallActList);
    }

    /**
     @ModelAttribute public SearchParam<MallActEntity> searchParam(SearchParam<MallActEntity> params, MallActEntity mallAct) {
     if (mallAct != null) {
     params.setEntity(mallAct);
     }

     return params;
     }
     **/
    /***********自定义开始***********/

    @Autowired
    private SumService sumService;
    @RequestMapping("/sum/top")
    public R sumTop(Long actId, Integer limit) {
        List<Object[]> data = sumService.sumTopUserBuy(actId, limit);
        return R.ok(data);
    }
}
